package com.wsb.boot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.rabbitmq.client.*;

/**
 * @author wang shaobo
 * @date 2020/7/20  17:04
 */
@Configuration
public class RabbitConfig {

    public static final String QUEUE_EMAIL = "queue_email";//email队列
    public static final String QUEUE_SMS = "queue_sms";//sms队列
    public static final String EXCHANGE_NAME="topic.exchange";//topics类型交换机
    public static final String ROUTINGKEY_EMAIL="topic.#.email.#";
    public static final String ROUTINGKEY_SMS="topic.#.sms.#";

    //声明交换机
    @Bean(EXCHANGE_NAME)
    public Exchange exchange(){
//        ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable().
//        ExchangeBuilder.
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable().build();

    }


    @Bean(QUEUE_EMAIL)
    public Queue emailQueue(){
        return new Queue(QUEUE_EMAIL,false);
    }

    //声明sms队列
    @Bean(QUEUE_SMS)
    public Queue smsQueue(){
        return new Queue(QUEUE_SMS,false);
    }


    //绑定交换机
    @Bean
    public Binding bindingEmail(@Qualifier(QUEUE_EMAIL) Queue queue,
                                @Qualifier(EXCHANGE_NAME) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();

    }

    //绑定交换机
    @Bean
    public Binding bindingSms(@Qualifier(QUEUE_SMS) Queue queue,
                                @Qualifier(EXCHANGE_NAME) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();

    }


}
